{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0db517e6-d0e6-401f-81e7-c7294be212a9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8db54c6-0e9d-457c-8eb4-8cbd79302e4f",
   "metadata": {},
   "source": [
    "用字典创建df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "05d25da1-51d4-460d-98cd-188278fe15f0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>country</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>aaa</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bbb</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ccc</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  country  population\n",
       "0     aaa          10\n",
       "1     bbb          12\n",
       "2     ccc          14"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\"country\":[\"aaa\", \"bbb\", \"ccc\"],\n",
    "       \"population\":[10, 12, 14]}\n",
    "df_data = pd.DataFrame(data)\n",
    "df_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d8e4d719-833b-4216-bb0f-f2ba10fb43df",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method DataFrame.info of   country  population\n",
       "0     aaa          10\n",
       "1     bbb          12\n",
       "2     ccc          14>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_data.info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1073cd2d-7a59-4564-a955-5044b69d1046",
   "metadata": {
    "tags": []
   },
   "source": [
    "读取文件中的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5a3b2ec5-8314-429c-a43d-29533d729cf0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    22.0\n",
       "1    38.0\n",
       "2    26.0\n",
       "3    35.0\n",
       "4    35.0\n",
       "Name: Age, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"titanic/train.csv\")\n",
    "age = df[\"Age\"]\n",
    "age[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54787918-7e1e-4c0a-8a78-71a8c555cfae",
   "metadata": {},
   "source": [
    "series是dataframe中的一行/列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "32b76475-55e2-4251-9f3d-733cada23991",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(age)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a414c62-70d2-41b9-945d-f6f76761fbfa",
   "metadata": {},
   "source": [
    "可以将某一列的数据改为索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c09c0c74-daa1-4167-a394-9638bab255c7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Braund, Mr. Owen Harris</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cumings, Mrs. John Bradley (Florence Briggs Thayer)</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Heikkinen, Miss. Laina</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Futrelle, Mrs. Jacques Heath (Lily May Peel)</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Allen, Mr. William Henry</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                    PassengerId  Survived  \\\n",
       "Name                                                                        \n",
       "Braund, Mr. Owen Harris                                       1         0   \n",
       "Cumings, Mrs. John Bradley (Florence Briggs Tha...            2         1   \n",
       "Heikkinen, Miss. Laina                                        3         1   \n",
       "Futrelle, Mrs. Jacques Heath (Lily May Peel)                  4         1   \n",
       "Allen, Mr. William Henry                                      5         0   \n",
       "\n",
       "                                                    Pclass     Sex   Age  \\\n",
       "Name                                                                       \n",
       "Braund, Mr. Owen Harris                                  3    male  22.0   \n",
       "Cumings, Mrs. John Bradley (Florence Briggs Tha...       1  female  38.0   \n",
       "Heikkinen, Miss. Laina                                   3  female  26.0   \n",
       "Futrelle, Mrs. Jacques Heath (Lily May Peel)             1  female  35.0   \n",
       "Allen, Mr. William Henry                                 3    male  35.0   \n",
       "\n",
       "                                                    SibSp  Parch  \\\n",
       "Name                                                               \n",
       "Braund, Mr. Owen Harris                                 1      0   \n",
       "Cumings, Mrs. John Bradley (Florence Briggs Tha...      1      0   \n",
       "Heikkinen, Miss. Laina                                  0      0   \n",
       "Futrelle, Mrs. Jacques Heath (Lily May Peel)            1      0   \n",
       "Allen, Mr. William Henry                                0      0   \n",
       "\n",
       "                                                              Ticket     Fare  \\\n",
       "Name                                                                            \n",
       "Braund, Mr. Owen Harris                                    A/5 21171   7.2500   \n",
       "Cumings, Mrs. John Bradley (Florence Briggs Tha...          PC 17599  71.2833   \n",
       "Heikkinen, Miss. Laina                              STON/O2. 3101282   7.9250   \n",
       "Futrelle, Mrs. Jacques Heath (Lily May Peel)                  113803  53.1000   \n",
       "Allen, Mr. William Henry                                      373450   8.0500   \n",
       "\n",
       "                                                   Cabin Embarked  \n",
       "Name                                                               \n",
       "Braund, Mr. Owen Harris                              NaN        S  \n",
       "Cumings, Mrs. John Bradley (Florence Briggs Tha...   C85        C  \n",
       "Heikkinen, Miss. Laina                               NaN        S  \n",
       "Futrelle, Mrs. Jacques Heath (Lily May Peel)        C123        S  \n",
       "Allen, Mr. William Henry                             NaN        S  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.set_index(\"Name\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "945acf35-130c-409d-8f04-8c4b3f5e0443",
   "metadata": {},
   "source": [
    "现在可以看到左侧不再是数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ee5fb829-4b5c-4880-9e3a-b22737ef8ee8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Name\n",
       "Braund, Mr. Owen Harris                                22.0\n",
       "Cumings, Mrs. John Bradley (Florence Briggs Thayer)    38.0\n",
       "Heikkinen, Miss. Laina                                 26.0\n",
       "Futrelle, Mrs. Jacques Heath (Lily May Peel)           35.0\n",
       "Allen, Mr. William Henry                               35.0\n",
       "Name: Age, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Age\"][:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f60511d0-a72b-46fc-b477-e72af4da9036",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Name\n",
       "Braund, Mr. Owen Harris                                22.0\n",
       "Cumings, Mrs. John Bradley (Florence Briggs Thayer)    38.0\n",
       "Heikkinen, Miss. Laina                                 26.0\n",
       "Futrelle, Mrs. Jacques Heath (Lily May Peel)           35.0\n",
       "Allen, Mr. William Henry                               35.0\n",
       "Name: Age, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age = df[\"Age\"]\n",
    "age[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5f9c536f-3afb-4e47-8f62-f1a1db1df221",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35.0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age[\"Allen, Mr. William Henry\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "61fb8139-2cf1-45fc-aa6d-1d6ef4c4fb5f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Name\n",
       "Braund, Mr. Owen Harris                                32.0\n",
       "Cumings, Mrs. John Bradley (Florence Briggs Thayer)    48.0\n",
       "Heikkinen, Miss. Laina                                 36.0\n",
       "Futrelle, Mrs. Jacques Heath (Lily May Peel)           45.0\n",
       "Allen, Mr. William Henry                               45.0\n",
       "Name: Age, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age[:5] + 10 #向量化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbfa22fe-d16d-4185-abc3-0515c56f4c00",
   "metadata": {
    "tags": []
   },
   "source": [
    "将有数值的列进行统计,得出平均值，最大值等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ac2c669a-3ebe-49f1-9c38-8b692a8ff1eb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>714.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>446.000000</td>\n",
       "      <td>0.383838</td>\n",
       "      <td>2.308642</td>\n",
       "      <td>29.699118</td>\n",
       "      <td>0.523008</td>\n",
       "      <td>0.381594</td>\n",
       "      <td>32.204208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>257.353842</td>\n",
       "      <td>0.486592</td>\n",
       "      <td>0.836071</td>\n",
       "      <td>14.526497</td>\n",
       "      <td>1.102743</td>\n",
       "      <td>0.806057</td>\n",
       "      <td>49.693429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.420000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>223.500000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>20.125000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>7.910400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>446.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>28.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>14.454200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>668.500000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>31.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>891.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>80.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>512.329200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       PassengerId    Survived      Pclass         Age       SibSp  \\\n",
       "count   891.000000  891.000000  891.000000  714.000000  891.000000   \n",
       "mean    446.000000    0.383838    2.308642   29.699118    0.523008   \n",
       "std     257.353842    0.486592    0.836071   14.526497    1.102743   \n",
       "min       1.000000    0.000000    1.000000    0.420000    0.000000   \n",
       "25%     223.500000    0.000000    2.000000   20.125000    0.000000   \n",
       "50%     446.000000    0.000000    3.000000   28.000000    0.000000   \n",
       "75%     668.500000    1.000000    3.000000   38.000000    1.000000   \n",
       "max     891.000000    1.000000    3.000000   80.000000    8.000000   \n",
       "\n",
       "            Parch        Fare  \n",
       "count  891.000000  891.000000  \n",
       "mean     0.381594   32.204208  \n",
       "std      0.806057   49.693429  \n",
       "min      0.000000    0.000000  \n",
       "25%      0.000000    7.910400  \n",
       "50%      0.000000   14.454200  \n",
       "75%      0.000000   31.000000  \n",
       "max      6.000000  512.329200  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c3ea928-c834-459b-b302-665ed62d4b18",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
